package com.icebartech.base.modules.common.facade.impl;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.icebartech.base.modules.common.dto.DictDTO;
import com.icebartech.base.modules.common.facade.SysCodeFacade;
import com.icebartech.base.modules.common.model.SysCode;
import com.icebartech.base.modules.common.service.SysCodeService;
import com.icebartech.core.enums.CommonResultCodeEnum;
import com.icebartech.core.vo.ResponseData;

@Service
public class SysCodeFacadeImpl implements SysCodeFacade {
	
	@Autowired
	private SysCodeService sysCodeService;

	@Override
	public ResponseData<List<DictDTO>> findSysCodeByType(String codeType) {
		ResponseData<List<DictDTO>> resp = ResponseData.builder(null, CommonResultCodeEnum.FAIL);
		List<SysCode> sysCodeList = sysCodeService.findSysCodeByType(codeType);
		List<DictDTO> dataList = new ArrayList<>();
		if(CollectionUtils.isNotEmpty(sysCodeList)){
			sysCodeList.forEach(sysCode -> {
				DictDTO dto = new DictDTO();
				dto.setKey(sysCode.getCodeId().toString());
				dto.setValue(sysCode.getCodeName());
				dataList.add(dto);
			});
		}
		resp = ResponseData.builder(dataList, CommonResultCodeEnum.SUCCESS);
		return resp;
	}

	@Override
	public ResponseData<DictDTO> findSysCodeByCodeId(String codeType, Long codeId) {
		ResponseData<DictDTO> resp = ResponseData.builder(null, CommonResultCodeEnum.FAIL);
		SysCode sysCode = sysCodeService.findSysCodeByCodeId(codeType, codeId);
		if(sysCode != null){
			DictDTO dto = new DictDTO();
			dto.setKey(sysCode.getCodeId().toString());
			dto.setValue(sysCode.getCodeName());
			resp = ResponseData.builder(dto, CommonResultCodeEnum.SUCCESS);
		}
		return resp;
	}

	
	

}
